<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<div id="ccc"></div>
<script>
  var base = "../src/pages/poster/template/price/1920/";
  var images = ["baqicaomeisuannai/images/cheng.png"];
  var uploadList = [];

  function createImageToBase64(data,token) {
    if (data.length == 0) {
      console.log(JSON.stringify(uploadList));
      return false;
    }
    var src = data[0];
    data.splice(0, 1);
    function getBase64Image(img) {
      var canvas = document.createElement("canvas");
      canvas.width = img.width;
      canvas.height = img.height;
      var ctx = canvas.getContext("2d");
      ctx.drawImage(img, 0, 0, img.width, img.height);
      var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
      var dataURL = canvas.toDataURL("image/" + ext);
      return dataURL.substring(22);
    }

    var image = new Image();
    image.src = base + src;
    image.onload = function () {
      var base64 = getBase64Image(image);
      putb64(base64, token,function (d) {
        uploadList.push({
          file: src.split('/images/')[0],
          name: src.split('/images/')[1],
          src: 'http://s.swao.cn/' + JSON.parse(d).hash
        });
        createImageToBase64(data);
      })
    }
  }

  $.ajax({
    type:'POST',
    url:'http://ljapi.swao.cn/qiniu/uptokenV2',
    dataType: 'json',
    contentType: 'application/json',
    success:function(res){
      createImageToBase64(images,res.data.uptoken);
    }
  });



  function putb64(base64, token, callbackFn) {
    var pic = base64;
    var url = "http://up.qiniu.com/putb64/-1"; //非华东空间需要根据注意事项 1 修改上传域名
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        callbackFn(xhr.responseText);
      }
    };
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-Type", "application/octet-stream");
    xhr.setRequestHeader("Authorization", "UpToken "+token);
    xhr.send(pic);
  }

</script>
</body>
</html>
